//index.js
const app = getApp()
const { envList } = require('../../envList.js')

Page({
    data: {
        groupGroup: ['请选择', '一', '二', '三', '四', '五'],
        isShow: true,
        type: "person",
        num: 0,
        value: [0],
        group_max_num: 0,
        person_max_num: 0,
        gender_max_num: 0,
        max_num: [],
        group: 'gender',
        redGroup: '男',
        redSelect: 0,
        blueGroup: '女',
        blueSelect: 0
    },
    onLoad: function (options) {
        // Do some initialize when page load.
    },
    onShow: function () {
        // Do something when page show.
    },
    onReady: function () {
        // Do something when page ready.
        this.setData({
            redGroup: this.data.groupGroup[this.data.redSelect],
            blueGroup: this.data.groupGroup[this.data.blueSelect]
        })
    },

    // Event handler.
    showView: function (e) {
        if(wx.getStorageSync('isAdmin') != true){
            wx.showModal({
                title: '验证权限',
                //content: '请输入管理员密码。',
                editable:true,
                success (res) {
                    console.log(res)
                  if (res.confirm == true) {
                   if(res.content == '123123'){
                   wx.setStorage({
                        key:"isAdmin",
                        data:true,
                        success:function(e){
                            wx.showToast({
                                title: '验证通过！',
                                icon: 'success',
                                duration: 2000
                              })
                              setTimeout(function(e){
                                wx.hideLoading()
                              },1500)
                        }
                      })
                   }else{
                    wx.showToast({
                        title: '验证未通过！',
                        icon: 'error',
                        duration: 2000
                      })
                      setTimeout(function(e){
                        wx.hideLoading()
                      },1500)
                   }

                  } else if (res.cancel) {
                    
                  }
                }
              })
        }else{
            this.setData({
                num: 0,
                value: [0],
                type: e.currentTarget.dataset.class
            })
            if (e.currentTarget.dataset.class != "quick") {
                if (this.data.group_max_num == 0 || this.data.person_max_num == 0 || this.data.gender_max_num == 0) {
                    wx.showLoading({
                        title: '加载中',
                    })
                    wx.cloud.callFunction({
                        name: 'classCounter',
                        data: {
                            type: 'getMaxNum',
                        }
                    }).then(res => {
                        console.log(res)
                        this.setData({
                            group_max_num: res.result.data.groupMaxNum,
                            person_max_num: res.result.data.personMaxNum,
                            gender_max_num: res.result.data.genderMaxNum,
                            isShow: false,
                            max_num: e.currentTarget.dataset.class == 'group' ? res.result.data.genderMaxNum : res.result.data.personMaxNum
                        });
                        wx.hideLoading()
                    }).catch(console.error)
                } else {
                    if (e.currentTarget.dataset.class == 'person') {
                        this.setData({
                            isShow: false,
                            max_num: this.data.person_max_num
                        });
                    }
                    else if (this.data.group == 'group' && e.currentTarget.dataset.class == 'group') {
                        console.log('group group')
                        this.setData({
                            isShow: false,
                            max_num: this.data.group_max_num
                        })
                    } else if (this.data.group == 'gender' && e.currentTarget.dataset.class == 'group') {
                        console.log('gender group')
                        this.setData({
                            isShow: false,
                            max_num: this.data.gender_max_num
                        })
                    } else {
                        this.setData({
                            isShow: false,
                            max_num: this.data.gender_max_num
                        })
                    }
    
                }
            } else {
                this.jumpPage()
            } 
        }
    },
    closeCoverView: function (e) {
        this.setData({
            isShow: true
        });
    },
    formSubmit(e) {
        console.log('form发生了submit事件，携带数据为：', e.detail.value)
        this.jumpPage(e);
    },
    formReset(e) {
        console.log('form发生了reset事件，携带数据为：', e.detail.value)
        this.setData({
            isShow: true
        })
    },
    getNum: function (e) {
        console.log(e)
        this.setData({
            num: e.detail.value[0],
            value: [e.detail.value[0]]
        })
    },
    getGroup: function (e) {
        console.log(e)
        this.setData({
            num: 0,
            value: [0],
            group: e.detail.value,
            max_num: e.detail.value == 'group' ? this.data.group_max_num : this.data.gender_max_num
        })

    },
    refalse: function (e) {
        this.setData({
            num: 0,
            value: [0]
        })
    },
    getGroupItem: function (e) {
        console.log(e)
        if (this.data.group == 'group') {
            this.setData({
                num: 0,
                value: [0],
                redGroup: this.data.groupGroup[e.detail.value[0]],
                redSelect: e.detail.value[0],
                blueSelect: e.detail.value[1],
                blueGroup: this.data.groupGroup[e.detail.value[1]]
            })
        }
    },
    //提交选择条件，跳转名单列表
    jumpPage(e) {
        if (this.data.type == 'person') {
            if (this.data.num == 0 || this.data.num == '0') {
                wx.showToast({
                    title: '请选择人数。',
                    icon: 'error',
                    duration: 2000
                })
            }else{
                wx.navigateTo({
                    url: `/pages/namelist/index?num=${this.data.num}`,
                })
            }
           
        } else if (this.data.type == 'group') {
            console.log(this.data.num)
            if (this.data.num == 0 || this.data.num == '0') {
                wx.showToast({
                    title: '请选择人数。',
                    icon: 'error',
                    duration: 2000
                })
            }
            else if (this.data.group == 'gender') {
                wx.navigateTo({
                    url: `/pages/counterlist/index?num=${this.data.num}&group=${this.data.group}&redGroup=男&blueGroup=女`
                })
            }
            else if (this.data.redSelect == 0 || this.data.blueSelect == 0) {
                wx.showToast({
                    title: '请选择小组。',
                    icon: 'error',
                    duration: 2000
                })

            } else if (this.data.redSelect == this.data.blueSelect) {
                wx.showToast({
                    title: '不能选择相同的小组。',
                    icon: 'error',
                    duration: 2000
                })
            }
            else {
                wx.navigateTo({
                    url: `/pages/counterlist/index?num=${this.data.num}&group=${this.data.group}&redGroup=${this.data.redGroup}&blueGroup=${this.data.blueGroup}`
                })
            }
        } else if (this.data.type == 'quick') {
            wx.navigateTo({
                url: '/pages/namelist/index?num=1',
            })
        }
    },
})